package transport

import (
	"crypto/tls"
	"net"
)

type HTTP2Transport struct {
	tlsConfig *tls.Config
}

func NewHTTP2Transport(certFile, keyFile string) (*HTTP2Transport, error) {
	cert, err := tls.LoadX509KeyPair(certFile, keyFile)
	if err != nil {
		return nil, err
	}
	return &HTTP2Transport{
		tlsConfig: &tls.Config{
			Certificates: []tls.Certificate{cert},
			NextProtos:   []string{"h2"},
		},
	}, nil
}

func (t *HTTP2Transport) Listen() (net.Listener, error) {
	return tls.Listen("tcp", ":443", t.tlsConfig)
}

func (t *HTTP2Transport) Dial(addr string) (net.Conn, error) {
	return tls.Dial("tcp", addr, &tls.Config{
		InsecureSkipVerify: true, // 生产环境需验证证书
		NextProtos:         []string{"h2"},
	})
}
